package database

import (
	"Software/global"
	"Software/model"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func DBInit() {

	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?%s",
		global.Config.GetString("mysql.name"),
		global.Config.GetString("mysql.password"),
		global.Config.GetString("mysql.address"),
		global.Config.GetString("mysql.port"),
		global.Config.GetString("mysql.database"),
		global.Config.GetString("mysql.config"),
	)

	//dsn := "root:040824@tcp(127.0.0.1:3306)/apk?charset=utf8mb4&parseTime=True&loc=Local"

	fmt.Println(dsn)
	// 初始化数据库时的高级配置
	db, err := gorm.Open(mysql.New(mysql.Config{
		// 数据库句柄
		DSN: dsn,
		// 数据库默认字符串长度
		DefaultStringSize: 256,
	}), &gorm.Config{})

	if err != nil {
		fmt.Println("err : " + err.Error())
	}
	_ = db.AutoMigrate(
		&model.Software{},
		&model.FeedBack{},
		&model.ChallengeRecord{},
		&model.Auditor{},
	)
	global.DB = db
}
